<?php
require_once 'role.php';

class phd extends role{
	public $connection;
	public function __construct($id){
		parent::__construct($id,'phd');
	}	
	public function homepage(){
		
		$data = array(
			"name" => "",
			"balance" => "",
			"renewdate" => ""
		);
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$query = "CALL get_phd_homepage('".$this->getID()."')";
		$result = mysqli_query($connection,$query);
		$temp = mysqli_fetch_array($result,MYSQLI_ASSOC);
		$data['name']=$temp['name'];
		$data['renewdate'] = date("d-m-Y", strtotime(date("Y-m-d", strtotime($temp['date_of_joining'])) . " + 365 day"));
		$data['balance']=$temp['balance'];
		return $data;
	}
	function getArchivedRequests(){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$tdata = array("req_id"=>"","item_name"=>"","item_cost"=>"","category"=>"","date"=>"","priority"=>"","advisor"=>"","status"=>"");
		$data = array($tdata);
		$i=0;
		$query = "CALL get_archived_requests('".$this->getID()."')";
		$result = mysqli_query($connection,$query);
		if(mysqli_num_rows($result)>=1){
		while($temp = mysqli_fetch_array($result,MYSQLI_ASSOC)){
			$tdata['req_id']=$temp['req_id'];
			$tdata['item_name']=$temp['item_name'];
			$tdata['item_cost']=$temp['item_cost'];
			$tdata['category']=ucfirst($temp['category']);
			$tdata['date']=$temp['time_stamp'];
			$tdata['priority']=ucfirst($temp['priority']);
			$tdata['advisor']=$temp['adv_id'];
			$tdata['status']=ucfirst($temp['status']);
			$data[$i]=$tdata;
			$i++;
		}
		return $data;	
		}
		else return NULL;
		
	}
	
	public function get_phd_inbox(){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$query="call get_messages('".$this->getID()."')";
	$result=mysqli_query($connection,$query);
	$data=array("from_id"=>"","req_id"=>"","time_stamp"=>"","content"=>"");
	$bdata=array($data);
	$i=0;
	while($d = mysqli_fetch_array($result,MYSQLI_ASSOC)){
		$data['from_id']=$d['from_id'];
	$data['req_id']=$d['req_id'];
	$data['time_stamp']=$d['time_stamp'];
	$data['content']=$d['content'];
	$bdata[i]=$data;
	$i++;
	}
	
	//$bdata=array($data);
			return $bdata;
	}
	
	public function getRequests(){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$query="call get_request_status_phd('".$this->getID()."')";
		$result = mysqli_query($connection,$query);
		$a=array("status"=>"","req_id"=>"","version_id"=>"");
		$b=array($a);		
		$i=0;
		$j=1;
		while($temp = mysqli_fetch_array($result,MYSQLI_ASSOC)){
			if($temp['status']<=3){
			$a['status']=$temp['status'];
			$a['req_id']=$temp['req_id'];
			$a['version_id']=$temp['version_id'];
			$b[$i]=$a;
			$i++;
			}
		}
		mysqli_free_result($result);
		if($i==0){
			return NULL;
		}
		$i=0;
		
		
		$tdata = array("req_id"=>"","item_name"=>"","item_cost"=>"","category"=>"","priority"=>"","date"=>"","advisor"=>"","status"=>"");
		$data = array($tdata);
		
		foreach($b as $t){
			$db1 = new DatabaseManager();
			$connection1 = $db1->getDB();
			$query = "call get_request_phd('".$this->getID()."',".$t['req_id'].",".$t['version_id'].")";
			$result = mysqli_query($connection1,$query);
			//if($result == FALSE) echo "FALSE"; else echo "TRUE";
			if($result){
				$temp = mysqli_fetch_array($result,MYSQLI_ASSOC);
				$tdata['req_id']=$temp['req_id'];
				$tdata['item_name']=$temp['item_name'];
				$tdata['item_cost']=$temp['item_cost'];
				$tdata['category']=ucfirst($temp['category']);
				$tdata['priority']=ucfirst($temp['priority']);
				$tdata['date']=$temp['time_stamp'];
				$tdata['advisor']=$this->getAdvisor();
				switch($t['status']){
					case 1:$tdata['status']="Pending at advisor";break;
					case 2:$tdata['status']="Pending at PGC Chair";break;
					case 3:$tdata['status']="Pending at office";break;
					default:$tdata['status']="Could not get status";break;
				}
				$data[$i]=$tdata;
				$i++;
			}
			//exit();
		}
		
		return $data;
		/*
		
		
		$tdata = array("req_id"=>"","item"=>"","itemcost"=>"","category"=>"","date"=>"","priority"=>"","advisor"=>"","status"=>"");
		$data = array($tdata);
		$i=0;
		$query = "CALL get_request_phd('".$this->getID()."')";
		$result = mysqli_query($connection,$query);
		if(mysqli_num_rows($result)>=1){
		while($temp = mysqli_fetch_array($result,MYSQLI_ASSOC)){
			$tdata['req_id']=$temp['req_id'];
			$tdata['item_name']=$temp['item_name'];
			$tdata['item_cost']=$temp['item_cost'];
			$tdata['category']=ucfirst($temp['category']);
			$tdata['date']=$temp['time_stamp'];
			$tdata['priority']=ucfirst($temp['priority']);
			$tdata['advisor']=$this->getAdvisor();
			$tdata['status']=ucfirst($temp['new_status']);
			$data[$i]=$tdata;
			$i++;
		}
		return $data;
		}
		else return NULL;
	*/	
	}
	public function submitRequests($data){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$q = "CALL request_fill('".$data['pid']."','".$data['adv']."','".$data['itemN']."',".$data['price'].",'".$data['itemselect']."','".$data['priority']."','".$data['filename']."','".$data['filetype']."','".$data['filesize']."','".$data['attachment']."','".$data['comments']."')";
		//$q= "call request_fill('p001','adv001','red',1,'stationary','high',NULL,'its working')";
		echo $q;
		$result = mysqli_query($connection,$q);
		$db1 = new DatabaseManager();
		$connection1 = $db1->getDB();
		//printf("Error: %s\n", mysqli_error($result));
		$query = "update registered set t1='1' where id='".$data['adv']."'";
		$result = mysqli_query($connection1,$query);
		return "done";
	}
	public function getAdvisor($bool = true){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$query = "CALL whois_adv('".$this->getID()."')";
		$result = mysqli_query($connection,$query);
		$temp  = mysqli_fetch_array($result,MYSQLI_ASSOC);
		mysqli_free_result($result);
		
		if($bool)
			return $temp['name'];
		else
			return $temp['adv_id'];
	}
}

?>